<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <title>Boost 库搜索引擎</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
 
        html,
        body {
            height: 100%;
            background: url('https://images.unsplash.com/photo-1517430816045-df4b7de6d0e6') no-repeat center center fixed;
            background-size: cover;
            font-family: Arial, sans-serif;
        }
 
        .container {
            width: 90%;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background-color: rgba(255, 255, 255, 0.9);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
 
        h1 {
            margin-bottom: 20px;
            font-size: 36px;
            color: #4e6ef2;
            text-align: center;
        }
 
        .search {
            display: flex;
            justify-content: center;
            position: relative;
        }
 
        .search input {
            flex: 1;
            height: 50px;
            border: 2px solid #ccc;
            padding-left: 10px;
            font-size: 17px;
            border-radius: 25px 0 0 25px;
            transition: border-color 0.3s;
        }
 
        .search input:focus {
            border-color: #4e6ef2;
            outline: none;
        }
 
        .search button {
            width: 160px;
            height: 50px;
            background-color: #4e6ef2;
            color: #fff;
            font-size: 19px;
            cursor: pointer;
            transition: background-color 0.3s;
            border: none;
            border-radius: 0 25px 25px 0;
        }
 
        .search button:hover {
            background-color: #3b5ec2;
        }
 
        .clear-btn {
            position: absolute;
            right: 170px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            font-size: 18px;
            display: none;
            color: #ccc;
        }
 
        .result {
            width: 100%;
            margin-top: 20px;
        }
 
        .result .item {
            margin-top: 15px;
            padding: 15px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 5px;
            transition: box-shadow 0.3s;
            text-align: left;
        }
 
        .result .item:hover {
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }
 
        .result .item a {
            display: block;
            text-decoration: none;
            font-size: 22px;
            color: #4e6ef2;
            margin-bottom: 5px;
        }
 
        .result .item a:hover {
            text-decoration: underline;
        }
 
        .result .item p {
            font-size: 16px;
            color: #333;
            margin-bottom: 5px;
        }
 
        .result .item i {
            display: block;
            font-style: normal;
            color: green;
        }
 
        .loader {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #4e6ef2;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 2s linear infinite;
            display: none;
            margin: 20px auto;
        }
 
        @keyframes spin {
            0% {
                transform: rotate(0deg);
            }
 
            100% {
                transform: rotate(360deg);
            }
        }
 
        .error-message {
            color: red;
            text-align: center;
            margin-top: 20px;
        }
 
        .pagination {
            margin-top: 20px;
            display: flex;
            justify-content: center;
        }
 
        .pagination button {
            background-color: #4e6ef2;
            color: #fff;
            border: none;
            border-radius: 5px;
            padding: 10px 15px;
            margin: 0 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
 
        .pagination button:hover {
            background-color: #3b5ec2;
        }
 
        .pagination button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
 
        .previous-searches {
            margin-top: 20px;
        }
 
        .previous-searches h2 {
            font-size: 20px;
            color: #4e6ef2;
            text-align: center;
            margin-bottom: 10px;
        }
 
        .previous-searches ul {
            list-style-type: none;
            text-align: center;
        }
 
        .previous-searches ul li {
            display: inline-block;
            margin: 5px 10px;
            padding: 5px 10px;
            background-color: #4e6ef2;
            color: #fff;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
 
        .previous-searches ul li:hover {
            background-color: #3b5ec2;
        }
    </style>
</head>
 
<body>
    <div class="container">
        <h1>Boost库搜索引擎</h1>
        <div class="search">
            <input type="text" placeholder="输入搜索关键字..." id="searchInput">
            <span class="clear-btn" id="clearBtn">&times;</span>
            <button onclick="Search()">搜索一下</button>
        </div>
        <div class="loader" id="loader"></div>
        <div class="result" id="resultContainer"></div>
        <div class="pagination" id="paginationContainer"></div>
        <div class="error-message" id="errorMessage"></div>
        <div class="previous-searches" id="previousSearches">
            <h2>之前的搜索</h2>
            <ul id="previousSearchList"></ul>
        </div>
    </div>
    <script>
        let currentPage = 1;
        const resultsPerPage = 8;
        let allResults = [];
        let previousSearches = JSON.parse(localStorage.getItem('previousSearches')) || [];
 
        $(document).ready(function () {
            $("#searchInput").on("input", function () {
                if ($(this).val()) {
                    $("#clearBtn").show();
                } else {
                    $("#clearBtn").hide();
                }
            });
 
            $("#clearBtn").on("click", function () {
                $("#searchInput").val('');
                $(this).hide();
            });
 
            displayPreviousSearches();
        });
 
        function Search() {
            const query = $("#searchInput").val().trim();
            if (!query) {
                alert("请输入搜索关键字！");
                return;
            }
 
            if (!previousSearches.includes(query)) {
                if (previousSearches.length >= 5) {
                    previousSearches.shift();
                }
                previousSearches.push(query);
                localStorage.setItem('previousSearches', JSON.stringify(previousSearches));
            }
 
            $("#loader").show();
            $("#errorMessage").text('');
            $.ajax({
                type: "GET",
                url: "/s?word=" + query,
                success: function (data) {
                    $("#loader").hide();
                    allResults = data;
                    currentPage = 1;
                    displayResults();
                },
                error: function () {
                    $("#loader").hide();
                    $("#errorMessage").text('搜索失败，请稍后重试。');
                }
            });
        }
 
        function displayResults() {
            const resultContainer = $("#resultContainer");
            const paginationContainer = $("#paginationContainer");
            resultContainer.empty();
            paginationContainer.empty();
 
            const totalResults = allResults.length;
            const totalPages = Math.ceil(totalResults / resultsPerPage);
 
            if (totalResults === 0) {
                $("#errorMessage").text('没有搜索到相关的内容。');
                return;
            }
 
            const start = (currentPage - 1) * resultsPerPage;
            const end = Math.min(start + resultsPerPage, totalResults);
            const currentResults = allResults.slice(start, end);
 
            currentResults.forEach(elem => {
                const item = $(`
                    <div class="item">
                        <a href="${elem.url}" target="_blank">${elem.title}</a>
                        <p>${elem.desc}</p>
                        <i>${elem.url}</i>
                    </div>
                `);
                resultContainer.append(item);
            });
 
            displayPagination(totalPages);
            displayPreviousSearches();
        }
 
        function displayPagination(totalPages) {
            const paginationContainer = $("#paginationContainer");
 
            if (currentPage > 1) {
                const prevButton = $('<button>上一页</button>');
                prevButton.on('click', function () {
                    currentPage--;
                    displayResults();
                });
                paginationContainer.append(prevButton);
            }
 
            let startPage, endPage;
            if (totalPages <= 5) {
                startPage = 1;
                endPage = totalPages;
            } else {
                if (currentPage <= 3) {
                    startPage = 1;
                    endPage = 5;
                } else if (currentPage + 2 >= totalPages) {
                    startPage = totalPages - 4;
                    endPage = totalPages;
                } else {
                    startPage = currentPage - 2;
                    endPage = currentPage + 2;
                }
            }
 
            for (let i = startPage; i <= endPage; i++) {
                const button = $(`<button>${i}</button>`);
                if (i === currentPage) {
                    button.prop('disabled', true);
                }
                button.on('click', function () {
                    currentPage = i;
                    displayResults();
                });
                paginationContainer.append(button);
            }
 
            if (currentPage < totalPages) {
                const nextButton = $('<button>下一页</button>');
                nextButton.on('click', function () {
                    currentPage++;
                    displayResults();
                });
                paginationContainer.append(nextButton);
            }
        }
 
        function displayPreviousSearches() {
            const previousSearchList = $("#previousSearchList");
            previousSearchList.empty();
 
            previousSearches.forEach(search => {
                const item = $(`<li>${search}</li>`);
                item.on('click', function () {
                    $("#searchInput").val(search);
                    Search();
                });
                previousSearchList.append(item);
            });
        }
    </script>
</body>
 
</html>